package com.example.checkqufuface.Server;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.example.baselibrary.Utils.Customer;
import com.example.baselibrary.Utils.SPUtils;
import com.example.baselibrary.Utils.StringUtil;
import com.example.baselibrary.Utils.ToastUtil;
import com.example.checkqufuface.RetrufitHelp.BaseSubscriber;
import com.example.checkqufuface.RetrufitHelp.ExceptionHandle;
import com.example.checkqufuface.RetrufitHelp.RetrofitClient;
import com.example.checkqufuface.bean.JJHgetAccessTokenByRefreshTokenBean;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

public class RefreshTokenService extends Service {
    private Timer timer;
    public String QUFU_Base_URL;
    public Map<String, String> headers;
    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                //对基础地址进行填写
                String baseUrl = (String) SPUtils.get(RefreshTokenService.this, Customer.QUFU_INPUT_URL, "");
                if (StringUtil.isNullOrEmpty(baseUrl)) {
                    QUFU_Base_URL = Customer.QUFU_BASE_URL;
                } else {
                    QUFU_Base_URL = baseUrl;
                }

                //获取headmap
                if (headers == null) {
                    headers = new HashMap<>();
                }
                String token = (String) SPUtils.get(RefreshTokenService.this,  Customer.QUFU_ACCESSTOKEN, "");

                if (!StringUtil.isNullOrEmpty(token)) {
                    headers.put("Token", token);
                }
                // 判断是否是登入的情况
                boolean isLogin = (boolean) SPUtils.get(RefreshTokenService.this,  Customer.QUFU_IS_LOGIN, true);
                if (!isLogin) {
                    refshToken();
                } else {
                    SPUtils.put(RefreshTokenService.this,Customer.QUFU_IS_LOGIN, false);
                }

            }
        }
    };

    private void refshToken() {
        String ss = (String) SPUtils.get(RefreshTokenService.this,  Customer.QUFU_REFRESHTOKEN, "");
        RetrofitClient.getInstance(RefreshTokenService.this, QUFU_Base_URL, headers).JJHgetAccessTokenByRefreshToken(ss, new BaseSubscriber<JJHgetAccessTokenByRefreshTokenBean>(RefreshTokenService.this) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable e) {
                ToastUtil.showToast("更新Token失败");

            }

            @Override
            public void onNext(JJHgetAccessTokenByRefreshTokenBean jjHgetAccessTokenByRefreshTokenBean) {
                if (jjHgetAccessTokenByRefreshTokenBean.getCode().equals("200")) {
                    SPUtils.put(RefreshTokenService.this,  Customer.QUFU_ACCESSTOKEN, jjHgetAccessTokenByRefreshTokenBean.getData().getToken());

                }

            }
        });
    }

    public RefreshTokenService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                // (1) 使用handler发送消息
                Message message = new Message();
                message.what = 0;
                mHandler.sendMessage(message);
            }
        }, 0, 1000 * 60 * 60);//每隔一秒使用handler发送一下消息,也就是每隔一秒执行一次,一直重复执行

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }
}
